<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="电影评论分类：二分类问题, Hmm">
    <meta name="description" content="说到深度学习，不可避免得会提及业界有哪些优秀的框架，Keras神经网络框架便是其中之一，它是一个高级神经网络APl，用Python编写，能够在TensorFlow，CNTK或Theano之上运行。它的开发重点是实现快速实验。能够以最小的延迟从理念到结果是进行良好研究的关键。接下来我们将要讲的神经网络原理与梯度求解，Keras都已经对它们有了很好的封装，在后续的学习中，大家只要学会怎么去构建网络结构就没有什么问题了，其余的问题都由神经网络框架替我们去解决。">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    
    <style>
        body{
            background-image: url(https://gitee.com/lyhcyt_admin/pic/raw/master/img/202204081828269.jpg);
            background-repeat:no-repeat;
            background-size: 100% 100%;
            background-attachment:fixed;
        }
    </style>


    <title>电影评论分类：二分类问题 | Hmm</title>
    <link rel="icon" type="image/png" href="/gitee.io/favicon.png">

    <link rel="stylesheet" type="text/css" href="/gitee.io/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/gitee.io/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/gitee.io/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/gitee.io/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/gitee.io/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/gitee.io/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/gitee.io/css/my.css">

    <script src="/gitee.io/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/gitee.io/atom.xml" title="Hmm" type="application/atom+xml">
<link rel="stylesheet" href="/gitee.io/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/gitee.io/css/prism-line-numbers.css" type="text/css"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <div>
                    <a href="/gitee.io/" class="waves-effect waves-light">
                        
                        <img src="./medias/loading1.gif" data-original="/gitee.io/medias/logo.png" class="logo-img" alt="LOGO">
                        
                        <span class="logo-span">Hmm</span>
                    </a>
                </div>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/gitee.io/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/gitee.io/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/gitee.io/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/gitee.io/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/gitee.io/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="./medias/loading1.gif" data-original="/gitee.io/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">Hmm</div>
        <div class="logo-desc">
            
            Never really desperate, only the lost of the soul.
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/gitee.io/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/gitee.io/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/gitee.io/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/gitee.io/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/gitee.io/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://gitee.com/lyhcyt_admin/gitee.io" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://gitee.com/lyhcyt_admin/gitee.io" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>


    



<div class="bg-cover pd-header post-cover" style="background-image: url('https://cdn.jsdelivr.net/gh/LiU-YU-HANG/blogpic@main/test1/202303031442619.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">电影评论分类：二分类问题</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/gitee.io/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/gitee.io/tags/keras/">
                                <span class="chip bg-color">keras</span>
                            </a>
                        
                            <a href="/gitee.io/tags/python/">
                                <span class="chip bg-color">python</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/gitee.io/categories/keras%E5%BF%AB%E9%80%9F%E6%90%AD%E5%BB%BA%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/" class="post-category">
                                keras快速搭建神经网络
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-04-05
                </div>
                

                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    13.8k
                </div>
                

                

                
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/gitee.io/libs/prism/prism.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h3 id="这个例子，可以将电影的评论文字内容将其划分为正面和负面">这个例子，可以将电影的评论文字内容将其划分为正面和负面</h3>
<p>IMDB数据集，它包含来自互联网电影数据库（IMDB）的50 000条严重两级分化的评论。数据集被分为用于训练的2500条评论与用于测试的2500条评论，训练集和测试集都包括50%的正面评论和50%的负面评论。<br>
IMDB数据集也内置于Keras库。它已经预处理：评论（单词序列）已经被转化为整数序列，其中每个整数代表字典中的某一个单词。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">#代码 3-1 加载IMDB数据集
from keras.datasets import imdb

(train_data, train_labels),(test_data, test_labels) &#x3D; imdb.load_data(num_words&#x3D;10000)<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">Using TensorFlow backend.<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>参数num_words=10000的意思是仅保留训练数据中前10000个最常出现的单词。低频单词将被舍弃。</p>
<p>train_data和test_data是由评论组成的列表，每条评论又是单词索引组成的列表。train_label和test_labels都是0和1组成的列表。0代表负面，1代表正面。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">train_data[0]<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">train_labels[0]<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<pre><code>1
</code></pre>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"># 由于限定为10000个常见的单词，单词索引都不会超过10000。
max([max(sequence) for sequence in train_data]) <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<pre><code>9999
</code></pre>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">word_index &#x3D; imdb.get_word_index() # word_index是一个将单词映射为整数索引的字典
reverse_word_index &#x3D; dict(         # 建值颠倒，将整数索引映射为单词
    [(value, key) for (key, value) in word_index.items()])
decoded_review &#x3D; &#39; &#39;.join(         # 将评论解码
    [reverse_word_index.get(i - 3, &#39;?&#39;) for i in train_data[0]])<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="准备数据-将数据向量化">准备数据 将数据向量化</h3>
<ul>
<li>填充列表，使其具有相同的长度将列表转换成形状为（samples, word_indices）的整张张量，然后网络第一层使用能处理这种张量的层（即Embedding层）</li>
<li>对列表进行one-hot编码，将其转化为0和1的向量，然后网络添加一层Dense层，它能够处理浮点数据。</li>
</ul>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">import numpy as np

def vectorize_sequences(sequences, dimension&#x3D;10000):
    results &#x3D; np.zeros((len(sequences), dimension))  # 创建一个形状为(len(sequences), dimension)的零矩阵
    for i, sequence in enumerate(sequences):
        results[i, sequence] &#x3D; 1.                    # 将results[i]的指定索引设为1
    return results

x_train &#x3D; vectorize_sequences(train_data) # 将训练数据向量化
x_test &#x3D; vectorize_sequences(test_data)   # 将测试数据向量化<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">x_train[0]<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">array([0., 1., 1., ..., 0., 0., 0.])<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h3 id="将标签向量化">将标签向量化</h3>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">y_train &#x3D; np.asarray(train_labels).astype(&#39;float32&#39;)
y_test &#x3D; np.asarray(test_labels).astype(&#39;float32&#39;)<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h3 id="构建网络">构建网络</h3>
<p>输入数据是向量，二标签是标量（1和0），这是你会遇到的最简单的情况。带有relu激活的全连接层（Dense）的简单堆叠，比如Dense(16, activation=‘relu’)。<br>
传入Dense层的参数（16）是该层的隐藏单元的个数，一个隐藏单元代表该层表示的一个维度。每个带有relu激活的Dense层都实现了下列张量运算：output = relu(dot(w, input) + b)<br>
对于Dense层的堆叠，需要确定两个关键架构</p>
<ul>
<li>网络有多少层</li>
<li>每层有多少个隐藏单元</li>
</ul>
<p>现在搭建下列架构：</p>
<ul>
<li>两个中间层，每层有16个隐藏单元；</li>
<li>第三层输出一个标量，预测当前评论情感；</li>
</ul>
<p>中间层使用relu作为激活函数，最后一层使用sigmoid激活以一个0 ~ 1范围内的概率值。relu函数将所有负值归0，而sigmoid函数则将任意值”压缩“到[0,1]区间内，其输出值可以看做概率值。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"># 代码3-3 模型定义
from keras import models
from keras import layers

model &#x3D; models.Sequential()
model.add(layers.Dense(16, activation&#x3D;&#39;relu&#39;, input_shape&#x3D;(10000,)))
model.add(layers.Dense(16, activation&#x3D;&#39;relu&#39;))
model.add(layers.Dense(1, activation&#x3D;&#39;sigmoid&#39;))<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>最后需要选择损失函数和优化器。由于这是一个二分类的问题，网络输出是一个概率值，那么最好使用binary_crossentropy（二元交叉熵）损失。这并不是唯一可行的选择，比如你还可以使用mean_squared_error（均方误差）。但对于输出概率值的模型，交叉熵（crossentropy）往往数最好的。<br>
交叉熵是来自于信息论领域的概念，用于衡量概率分布之间的距离，在这个例子中就是真实分布与预测值之间的距离</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"># 代码3-4 编译模型
model.compile(optimizer&#x3D;&#39;rmsprop&#39;,           #这里的优化器使用rmsprop
             loss&#x3D;&#39;binary_crossentropy&#39;,     #损失函数使用二元交叉熵
             mitrics&#x3D;[&#39;accuracy&#39;])           #这里我们在训练的过程中监控精度<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<p>如果不想使用Keras优化器、损失函数的默认设置，我门可以通过向optimizer参数传入一个优化器类实例来实现。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">#代码3-5 配置优化器
from keras import optimizers

model.compile(optimizer&#x3D;optimizers.RMSprop(lr&#x3D;0.001),
             loss&#x3D;&#39;binary_crossentropy&#39;,
             mitrics&#x3D;[&#39;accruacy&#39;])<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"># 代码3-6 使用自定义的损失和指标
from keras import losses
from keras import metrics

model.compile(optimizer&#x3D;optimizers.RMSprop(lr&#x3D;0.001),
             loss&#x3D;losses.binary_crossentropy,
             metrics&#x3D;[metrics.binary_accuracy])<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="验证你的方法">验证你的方法</h3>
<p>为了在训练过程中监控模型在前所未见的数据上的精度，需要将原始训练数据流出10000个样本作为验证集。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"># 代码3-7 留出验证集
x_val &#x3D; x_train[:10000]
partial_x_train &#x3D; x_train[10000:]

y_val &#x3D; y_train[:10000]
partial_y_train &#x3D; y_train[10000:]<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>现在使用512个样本组成小批量，将模型训练20个轮次（即对x_train和y_train两个张量中的所有样本进行20次迭代）。于此同时，你还要监控留出的10000个样本上的损失精度。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"># 代码3-8 训练模型
model.compile(optimizer&#x3D;&#39;rmsprop&#39;,
              loss&#x3D;&#39;binary_crossentropy&#39;,
              metrics&#x3D;[&#39;acc&#39;])

history &#x3D; model.fit(partial_x_train,
                    partial_y_train,
                    epochs&#x3D;20,
                    batch_size&#x3D;512,
                    validation_data&#x3D;(x_val, y_val))<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">Train on 15000 samples, validate on 10000 samples
Epoch 1&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 14s 953us&#x2F;step - loss: 0.5261 - acc: 0.7810 - val_loss: 0.3912 - val_acc: 0.8660
Epoch 2&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 279us&#x2F;step - loss: 0.3044 - acc: 0.9069 - val_loss: 0.3159 - val_acc: 0.8800
Epoch 3&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 196us&#x2F;step - loss: 0.2263 - acc: 0.9257 - val_loss: 0.2904 - val_acc: 0.8842
Epoch 4&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 201us&#x2F;step - loss: 0.1765 - acc: 0.9425 - val_loss: 0.2731 - val_acc: 0.8895
Epoch 5&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 211us&#x2F;step - loss: 0.1462 - acc: 0.9547 - val_loss: 0.2887 - val_acc: 0.8839
Epoch 6&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 211us&#x2F;step - loss: 0.1199 - acc: 0.9623 - val_loss: 0.3087 - val_acc: 0.8819
Epoch 7&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 199us&#x2F;step - loss: 0.0974 - acc: 0.9718 - val_loss: 0.3255 - val_acc: 0.8807
Epoch 8&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 183us&#x2F;step - loss: 0.0828 - acc: 0.9745 - val_loss: 0.3516 - val_acc: 0.8779
Epoch 9&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 181us&#x2F;step - loss: 0.0688 - acc: 0.9812 - val_loss: 0.3612 - val_acc: 0.8803
Epoch 10&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 186us&#x2F;step - loss: 0.0593 - acc: 0.9845 - val_loss: 0.3745 - val_acc: 0.8777
Epoch 11&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 191us&#x2F;step - loss: 0.0461 - acc: 0.9889 - val_loss: 0.4029 - val_acc: 0.8761
Epoch 12&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 204us&#x2F;step - loss: 0.0378 - acc: 0.9921 - val_loss: 0.4312 - val_acc: 0.8757
Epoch 13&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 184us&#x2F;step - loss: 0.0316 - acc: 0.9937 - val_loss: 0.4642 - val_acc: 0.8698
Epoch 14&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 187us&#x2F;step - loss: 0.0248 - acc: 0.9957 - val_loss: 0.4930 - val_acc: 0.8732
Epoch 15&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 199us&#x2F;step - loss: 0.0205 - acc: 0.9962 - val_loss: 0.5239 - val_acc: 0.8683
Epoch 16&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 189us&#x2F;step - loss: 0.0144 - acc: 0.9985 - val_loss: 0.6138 - val_acc: 0.8640
Epoch 17&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 207us&#x2F;step - loss: 0.0115 - acc: 0.9990 - val_loss: 0.5926 - val_acc: 0.8675
Epoch 18&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 185us&#x2F;step - loss: 0.0119 - acc: 0.9979 - val_loss: 0.6226 - val_acc: 0.8678
Epoch 19&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 183us&#x2F;step - loss: 0.0057 - acc: 0.9998 - val_loss: 0.6616 - val_acc: 0.8662
Epoch 20&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 197us&#x2F;step - loss: 0.0079 - acc: 0.9993 - val_loss: 0.6924 - val_acc: 0.8668<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>在CPU上运行，每轮的时间不到2秒，训练过程将在20秒内结束。每轮结束时会有短暂的停歇，因为模型要计算在验证集的10000个样本上的损失和精度。</p>
<p>model.fit()返回了一个 History 对象。这个对象有一个成员history，它是一个字典，包含训练过程中所有数据。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">history_dict &#x3D; history.history
history_dict.keys()<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">dict_keys([&#39;val_loss&#39;, &#39;val_acc&#39;, &#39;loss&#39;, &#39;acc&#39;])<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>字典中包含4个条目，对应训练过程和验证过程监控的指标</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"># 代码3-9 绘制训练损失和验证损失
import matplotlib.pyplot as plt

history_dict &#x3D; history.history
loss_values &#x3D; history_dict[&#39;loss&#39;]
val_loss_values &#x3D; history_dict[&#39;val_loss&#39;]

epochs &#x3D; range(1, len(loss_values) + 1)

plt.plot(epochs, loss_values, &#39;bo&#39;, label&#x3D;&#39;Training loss&#39;)       # 绘制训练损失。&#39;bo&#39;表示蓝色圆点
plt.plot(epochs, val_loss_values, &#39;b&#39;, label&#x3D;&#39;validation loss&#39;)  # 绘制验证损失。&#39;b&#39;表示蓝色实线
plt.title(&#39;Training and validation loss&#39;)
plt.xlabel(&#39;Epochs&#39;)    # x轴代表优化周期
plt.ylabel(&#39;Loss&#39;)      # y轴代表损失值
plt.legend()            # 显示图例

plt.show()<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="./medias/loading1.gif" data-original="https://cdn.jsdelivr.net/gh/LiU-YU-HANG/blogpic@main/test1/202303022308873.png" alt=""></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"># 代码3-10 绘制训练精度和验证精度
plt.clf()
acc &#x3D; history_dict[&#39;acc&#39;]
val_acc &#x3D; history_dict[&#39;val_acc&#39;]

plt.plot(epochs, acc, &#39;bo&#39;, label&#x3D;&#39;Training acc&#39;)
plt.plot(epochs, val_loss_values, &#39;b&#39;, label&#x3D;&#39;Validation acc&#39;)
plt.title(&#39;Training and validation accuracy&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;Accuracy&#39;)
plt.legend()<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">&lt;matplotlib.legend.Legend at 0x1cc1c8c6b88&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><img src="./medias/loading1.gif" data-original="https://cdn.jsdelivr.net/gh/LiU-YU-HANG/blogpic@main/test1/202303022308799.png" alt=""></p>
<p>如图所示，训练损失每轮都在降低，训练精度每轮都在提升。这就是梯度下降优化的预期结果————最小化的量随着每次迭代越来越小。但验证损失和验证精度似乎在第四轮后达到最佳值。造成了过拟合（overfit），在第二轮之后数据就开始过度优化。<br>
为了防止过拟合我们可以在第三轮之后停止训练</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"># 代码3-11 从头开始重新训练一个模型
model &#x3D; models.Sequential()
model.add(layers.Dense(16, activation&#x3D;&#39;relu&#39;, input_shape&#x3D;(10000,)))
model.add(layers.Dense(16, activation&#x3D;&#39;relu&#39;))
model.add(layers.Dense(1, activation&#x3D;&#39;sigmoid&#39;))

model.compile(optimizer&#x3D;&#39;rmsprop&#39;,
              loss&#x3D;&#39;binary_crossentropy&#39;,
              metrics&#x3D;[&#39;accuracy&#39;])

model.fit(x_train, y_train, epochs&#x3D;4, batch_size&#x3D;512)

results &#x3D; model.evaluate(x_test, y_test)<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">Epoch 1&#x2F;4
25000&#x2F;25000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 13s 500us&#x2F;step - loss: 0.5063 - accuracy: 0.7933
Epoch 2&#x2F;4
25000&#x2F;25000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 163us&#x2F;step - loss: 0.2963 - accuracy: 0.9056
Epoch 3&#x2F;4
25000&#x2F;25000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 157us&#x2F;step - loss: 0.2182 - accuracy: 0.9264
Epoch 4&#x2F;4
25000&#x2F;25000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 135us&#x2F;step - loss: 0.1781 - accuracy: 0.9389
25000&#x2F;25000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 15s 591us&#x2F;step<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">results<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">[0.2885350226783752, 0.8849200010299683]<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h3 id="进一步实验">进一步实验</h3>
<p>前面使用了两个隐藏层。可以尝试使用一个或三个隐藏层。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">### 使用一个隐藏层 ###
# 分出验证集和训练集
x_val &#x3D; x_train[:10000]
partial_x_train &#x3D; x_train[10000:]

y_val &#x3D; y_train[:10000]
partial_y_train &#x3D; y_train[10000:]

model &#x3D; models.Sequential()
model.add(layers.Dense(16, activation&#x3D;&#39;relu&#39;, input_shape&#x3D;(10000,)))
model.add(layers.Dense(1, activation&#x3D;&#39;sigmoid&#39;))

model.compile(optimizer&#x3D;&#39;rmsprop&#39;,
              loss&#x3D;&#39;binary_crossentropy&#39;,
              metrics&#x3D;[&#39;acc&#39;])

history &#x3D; model.fit(partial_x_train,
                    partial_y_train,
                    epochs&#x3D;20,
                    batch_size&#x3D;512,
                    validation_data&#x3D;(x_val, y_val))

results &#x3D; model.evaluate(x_test, y_test) #评估精度

history_dict &#x3D; history.history #得到训练历史数据，dict_keys([&#39;val_loss&#39;, &#39;val_acc&#39;, &#39;loss&#39;, &#39;acc&#39;])
loss_values &#x3D; history_dict[&#39;loss&#39;]
val_loss_valuse &#x3D; history_dict[&#39;val_loss&#39;]

acc &#x3D; history_dict[&#39;acc&#39;]
val_acc &#x3D; history_dict[&#39;val_acc&#39;]

epochs &#x3D; range(1, len(loss_values) + 1)

### 开始绘制 ###
# 绘制训练损失和验证损失图
plt.sca(plt.subplot(2,2,2))
plt.plot(epochs, loss_values, &#39;bo&#39;, label&#x3D;&#39;Training loss&#39;)
plt.plot(epochs, val_loss_valuse, &#39;b&#39;, label&#x3D;&#39;Validation loss&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;loss&#39;)
plt.title(&#39;Training and validation loss&#39;)
plt.legend()
# 绘制训练精度和验证精度图
plt.sca(plt.subplot(2,2,1))
plt.plot(epochs, val_acc, &#39;r&#39;, label&#x3D;&#39;Validation acc&#39;)
plt.plot(epochs, acc, &#39;ro&#39;, label&#x3D;&#39;Training acc&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;acc&#39;)
plt.title(&#39;Training and validation accuracy&#39;)
plt.legend()
# 绘制模型返回损失和精度图
plt.sca(plt.subplot(2,1,2))
name_list &#x3D; [&#39;results loss&#39;,&#39;results acc&#39;]
num_list &#x3D; results
plt.barh(range(len(num_list)), num_list,tick_label &#x3D; name_list)
plt.title(&#39;Results loss and accuracy&#39;)

plt.show()<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">Train on 15000 samples, validate on 10000 samples
Epoch 1&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 12s 770us&#x2F;step - loss: 0.5156 - acc: 0.7947 - val_loss: 0.4102 - val_acc: 0.8553
Epoch 2&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 5s 338us&#x2F;step - loss: 0.3337 - acc: 0.8993 - val_loss: 0.3326 - val_acc: 0.8807
Epoch 3&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 254us&#x2F;step - loss: 0.2580 - acc: 0.9209 - val_loss: 0.2950 - val_acc: 0.8907
Epoch 4&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 195us&#x2F;step - loss: 0.2108 - acc: 0.9355 - val_loss: 0.2856 - val_acc: 0.8874
Epoch 5&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 198us&#x2F;step - loss: 0.1790 - acc: 0.9466 - val_loss: 0.2734 - val_acc: 0.8911
Epoch 6&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 188us&#x2F;step - loss: 0.1553 - acc: 0.9544 - val_loss: 0.2780 - val_acc: 0.8889
Epoch 7&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 177us&#x2F;step - loss: 0.1360 - acc: 0.9612 - val_loss: 0.2780 - val_acc: 0.8894
Epoch 8&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 189us&#x2F;step - loss: 0.1195 - acc: 0.9674 - val_loss: 0.2832 - val_acc: 0.8878
Epoch 9&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 200us&#x2F;step - loss: 0.1064 - acc: 0.9708 - val_loss: 0.2929 - val_acc: 0.8874
Epoch 10&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 193us&#x2F;step - loss: 0.0926 - acc: 0.9761 - val_loss: 0.3050 - val_acc: 0.8827
Epoch 11&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 188us&#x2F;step - loss: 0.0836 - acc: 0.9785 - val_loss: 0.3149 - val_acc: 0.8840
Epoch 12&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 194us&#x2F;step - loss: 0.0738 - acc: 0.9815 - val_loss: 0.3273 - val_acc: 0.8836
Epoch 13&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 190us&#x2F;step - loss: 0.0655 - acc: 0.9857 - val_loss: 0.3427 - val_acc: 0.8763
Epoch 14&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 187us&#x2F;step - loss: 0.0580 - acc: 0.9872 - val_loss: 0.3551 - val_acc: 0.8805
Epoch 15&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 233us&#x2F;step - loss: 0.0521 - acc: 0.9893 - val_loss: 0.3683 - val_acc: 0.8789
Epoch 16&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 193us&#x2F;step - loss: 0.0455 - acc: 0.9913 - val_loss: 0.3854 - val_acc: 0.8784
Epoch 17&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 206us&#x2F;step - loss: 0.0402 - acc: 0.9931 - val_loss: 0.4044 - val_acc: 0.8769
Epoch 18&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 195us&#x2F;step - loss: 0.0356 - acc: 0.9942 - val_loss: 0.4446 - val_acc: 0.8734
Epoch 19&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 193us&#x2F;step - loss: 0.0307 - acc: 0.9955 - val_loss: 0.4356 - val_acc: 0.8745
Epoch 20&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 205us&#x2F;step - loss: 0.0273 - acc: 0.9965 - val_loss: 0.4526 - val_acc: 0.8710
25000&#x2F;25000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 10s 406us&#x2F;step<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="./medias/loading1.gif" data-original="https://cdn.jsdelivr.net/gh/LiU-YU-HANG/blogpic@main/test1/202303022309019.png" alt=""></p>
<ul>
<li>分析精度图可以看出在后期，训练精度趋势下降，验证精度有下降趋势。</li>
<li>分析损失图可以看出在后期，训练损失值越来越小，验证损失越来越大。</li>
<li>由此可以看出模型过拟化</li>
</ul>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">### 使用三个隐藏层 ###
# 分出验证集和训练集
x_val &#x3D; x_train[:10000]
partial_x_train &#x3D; x_train[10000:]

y_val &#x3D; y_train[:10000]
partial_y_train &#x3D; y_train[10000:]

model &#x3D; models.Sequential()
model.add(layers.Dense(16, activation&#x3D;&#39;relu&#39;, input_shape&#x3D;(10000,)))
model.add(layers.Dense(16, activation&#x3D;&#39;relu&#39;))
model.add(layers.Dense(16, activation&#x3D;&#39;relu&#39;))
model.add(layers.Dense(1, activation&#x3D;&#39;sigmoid&#39;))

model.compile(optimizer&#x3D;&#39;rmsprop&#39;,
              loss&#x3D;&#39;binary_crossentropy&#39;,
              metrics&#x3D;[&#39;acc&#39;])

history &#x3D; model.fit(partial_x_train,
                    partial_y_train,
                    epochs&#x3D;20,
                    batch_size&#x3D;512,
                    validation_data&#x3D;(x_val, y_val))

results &#x3D; model.evaluate(x_test, y_test) #评估精度

history_dict &#x3D; history.history #得到训练历史数据，dict_keys([&#39;val_loss&#39;, &#39;val_acc&#39;, &#39;loss&#39;, &#39;acc&#39;])
loss_values &#x3D; history_dict[&#39;loss&#39;]
val_loss_valuse &#x3D; history_dict[&#39;val_loss&#39;]

acc &#x3D; history_dict[&#39;acc&#39;]
val_acc &#x3D; history_dict[&#39;val_acc&#39;]

epochs &#x3D; range(1, len(loss_values) + 1)

### 开始绘制 ###
# 绘制训练损失和验证损失图
plt.sca(plt.subplot(2,2,2))
plt.plot(epochs, loss_values, &#39;bo&#39;, label&#x3D;&#39;Training loss&#39;)
plt.plot(epochs, val_loss_valuse, &#39;b&#39;, label&#x3D;&#39;Validation loss&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;loss&#39;)
plt.title(&#39;Training and validation loss&#39;)
plt.legend()
# 绘制训练精度和验证精度图
plt.sca(plt.subplot(2,2,1))
plt.plot(epochs, val_acc, &#39;r&#39;, label&#x3D;&#39;Validation acc&#39;)
plt.plot(epochs, acc, &#39;ro&#39;, label&#x3D;&#39;Training acc&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;acc&#39;)
plt.title(&#39;Training and validation accuracy&#39;)
plt.legend()
# 绘制模型返回损失和精度图
plt.sca(plt.subplot(2,1,2))
name_list &#x3D; [&#39;results loss&#39;,&#39;results acc&#39;]
num_list &#x3D; results
plt.barh(range(len(num_list)), num_list,tick_label &#x3D; name_list)
plt.title(&#39;Results loss and accuracy&#39;)

plt.show()<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">Train on 15000 samples, validate on 10000 samples
Epoch 1&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 14s 928us&#x2F;step - loss: 0.5195 - acc: 0.7827 - val_loss: 0.3729 - val_acc: 0.8699
Epoch 2&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 226us&#x2F;step - loss: 0.2872 - acc: 0.9037 - val_loss: 0.3164 - val_acc: 0.8731
Epoch 3&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 181us&#x2F;step - loss: 0.2006 - acc: 0.9313 - val_loss: 0.3482 - val_acc: 0.8619
Epoch 4&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 188us&#x2F;step - loss: 0.1620 - acc: 0.9453 - val_loss: 0.2900 - val_acc: 0.8839
Epoch 5&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 201us&#x2F;step - loss: 0.1320 - acc: 0.9566 - val_loss: 0.3026 - val_acc: 0.8840
Epoch 6&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 191us&#x2F;step - loss: 0.1019 - acc: 0.9693 - val_loss: 0.3425 - val_acc: 0.8741
Epoch 7&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 179us&#x2F;step - loss: 0.0844 - acc: 0.9747 - val_loss: 0.3834 - val_acc: 0.8757
Epoch 8&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 179us&#x2F;step - loss: 0.0734 - acc: 0.9775 - val_loss: 0.4504 - val_acc: 0.8658
Epoch 9&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 185us&#x2F;step - loss: 0.0550 - acc: 0.9850 - val_loss: 0.4089 - val_acc: 0.8741
Epoch 10&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 203us&#x2F;step - loss: 0.0447 - acc: 0.9887 - val_loss: 0.4564 - val_acc: 0.8731
Epoch 11&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 224us&#x2F;step - loss: 0.0373 - acc: 0.9901 - val_loss: 0.5071 - val_acc: 0.8623
Epoch 12&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 183us&#x2F;step - loss: 0.0315 - acc: 0.9920 - val_loss: 0.5063 - val_acc: 0.8692
Epoch 13&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 182us&#x2F;step - loss: 0.0251 - acc: 0.9928 - val_loss: 0.5353 - val_acc: 0.8711
Epoch 14&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 185us&#x2F;step - loss: 0.0116 - acc: 0.9989 - val_loss: 0.7878 - val_acc: 0.8443
Epoch 15&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 195us&#x2F;step - loss: 0.0217 - acc: 0.9951 - val_loss: 0.6993 - val_acc: 0.8481
Epoch 16&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 196us&#x2F;step - loss: 0.0075 - acc: 0.9993 - val_loss: 0.6340 - val_acc: 0.8669
Epoch 17&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 194us&#x2F;step - loss: 0.0080 - acc: 0.9989 - val_loss: 0.8637 - val_acc: 0.8520
Epoch 18&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 225us&#x2F;step - loss: 0.0044 - acc: 0.9997 - val_loss: 0.7075 - val_acc: 0.8661
Epoch 19&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 204us&#x2F;step - loss: 0.0190 - acc: 0.9942 - val_loss: 0.7546 - val_acc: 0.8688
Epoch 20&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 235us&#x2F;step - loss: 0.0020 - acc: 0.9998 - val_loss: 0.7678 - val_acc: 0.8674
25000&#x2F;25000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 10s 418us&#x2F;step<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="./medias/loading1.gif" data-original="https://cdn.jsdelivr.net/gh/LiU-YU-HANG/blogpic@main/test1/202303022309222.png" alt=""></p>
<ul>
<li>分析精度图可以看出在后期，训练精度趋势下降，验证精度不稳定。</li>
<li>分析损失图可以看出在后期，训练损失值越来越小，验证损失越来越大。</li>
<li>由此可以看出增加层后模型过拟化加重</li>
</ul>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">### 使用32个隐藏神经元 ###
# 分出验证集和训练集
x_val &#x3D; x_train[:10000]
partial_x_train &#x3D; x_train[10000:]

y_val &#x3D; y_train[:10000]
partial_y_train &#x3D; y_train[10000:]

model &#x3D; models.Sequential()
model.add(layers.Dense(32, activation&#x3D;&#39;relu&#39;, input_shape&#x3D;(10000,)))
model.add(layers.Dense(32, activation&#x3D;&#39;relu&#39;))
model.add(layers.Dense(1, activation&#x3D;&#39;sigmoid&#39;))

model.compile(optimizer&#x3D;&#39;rmsprop&#39;,
              loss&#x3D;&#39;binary_crossentropy&#39;,
              metrics&#x3D;[&#39;acc&#39;])

history &#x3D; model.fit(partial_x_train,
                    partial_y_train,
                    epochs&#x3D;20,
                    batch_size&#x3D;512,
                    validation_data&#x3D;(x_val, y_val))

results &#x3D; model.evaluate(x_test, y_test) #评估精度

history_dict &#x3D; history.history #得到训练历史数据，dict_keys([&#39;val_loss&#39;, &#39;val_acc&#39;, &#39;loss&#39;, &#39;acc&#39;])
loss_values &#x3D; history_dict[&#39;loss&#39;]
val_loss_valuse &#x3D; history_dict[&#39;val_loss&#39;]

acc &#x3D; history_dict[&#39;acc&#39;]
val_acc &#x3D; history_dict[&#39;val_acc&#39;]

epochs &#x3D; range(1, len(loss_values) + 1)

### 开始绘制 ###
# 绘制训练损失和验证损失图
plt.sca(plt.subplot(2,2,2))
plt.plot(epochs, loss_values, &#39;bo&#39;, label&#x3D;&#39;Training loss&#39;)
plt.plot(epochs, val_loss_valuse, &#39;b&#39;, label&#x3D;&#39;Validation loss&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;loss&#39;)
plt.title(&#39;Training and validation loss&#39;)
plt.legend()
# 绘制训练精度和验证精度图
plt.sca(plt.subplot(2,2,1))
plt.plot(epochs, val_acc, &#39;r&#39;, label&#x3D;&#39;Validation acc&#39;)
plt.plot(epochs, acc, &#39;ro&#39;, label&#x3D;&#39;Training acc&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;acc&#39;)
plt.title(&#39;Training and validation accuracy&#39;)
plt.legend()
# 绘制模型返回损失和精度图
plt.sca(plt.subplot(2,1,2))
name_list &#x3D; [&#39;results loss&#39;,&#39;results acc&#39;]
num_list &#x3D; results
plt.barh(range(len(num_list)), num_list,tick_label &#x3D; name_list)
plt.title(&#39;Results loss and accuracy&#39;)

plt.show()<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">Train on 15000 samples, validate on 10000 samples
Epoch 1&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 13s 843us&#x2F;step - loss: 0.4870 - acc: 0.7880 - val_loss: 0.3458 - val_acc: 0.8812
Epoch 2&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 231us&#x2F;step - loss: 0.2762 - acc: 0.9062 - val_loss: 0.3610 - val_acc: 0.8508
Epoch 3&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 214us&#x2F;step - loss: 0.2081 - acc: 0.9295 - val_loss: 0.2804 - val_acc: 0.8879
Epoch 4&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 207us&#x2F;step - loss: 0.1556 - acc: 0.9480 - val_loss: 0.2854 - val_acc: 0.8866
Epoch 5&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 202us&#x2F;step - loss: 0.1246 - acc: 0.9592 - val_loss: 0.3149 - val_acc: 0.8804
Epoch 6&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 207us&#x2F;step - loss: 0.1045 - acc: 0.9670 - val_loss: 0.3090 - val_acc: 0.8836
Epoch 7&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 244us&#x2F;step - loss: 0.0799 - acc: 0.9757 - val_loss: 0.3354 - val_acc: 0.8824
Epoch 8&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 206us&#x2F;step - loss: 0.0610 - acc: 0.9830 - val_loss: 0.3648 - val_acc: 0.8780
Epoch 9&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 200us&#x2F;step - loss: 0.0497 - acc: 0.9873 - val_loss: 0.4507 - val_acc: 0.8634
Epoch 10&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 201us&#x2F;step - loss: 0.0362 - acc: 0.9910 - val_loss: 0.4227 - val_acc: 0.8776
Epoch 11&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 201us&#x2F;step - loss: 0.0316 - acc: 0.9922 - val_loss: 0.4707 - val_acc: 0.8706
Epoch 12&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 250us&#x2F;step - loss: 0.0253 - acc: 0.9941 - val_loss: 0.4909 - val_acc: 0.8731
Epoch 13&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 215us&#x2F;step - loss: 0.0105 - acc: 0.9991 - val_loss: 0.5456 - val_acc: 0.8673
Epoch 14&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 208us&#x2F;step - loss: 0.0192 - acc: 0.9951 - val_loss: 0.5708 - val_acc: 0.8727
Epoch 15&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 207us&#x2F;step - loss: 0.0050 - acc: 0.9998 - val_loss: 0.6069 - val_acc: 0.8715
Epoch 16&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 200us&#x2F;step - loss: 0.0104 - acc: 0.9974 - val_loss: 0.6386 - val_acc: 0.8689
Epoch 17&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 225us&#x2F;step - loss: 0.0026 - acc: 0.9999 - val_loss: 0.6836 - val_acc: 0.8684
Epoch 18&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 225us&#x2F;step - loss: 0.0084 - acc: 0.9972 - val_loss: 0.7139 - val_acc: 0.8688
Epoch 19&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 205us&#x2F;step - loss: 0.0014 - acc: 0.9999 - val_loss: 0.7609 - val_acc: 0.8664
Epoch 20&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 219us&#x2F;step - loss: 0.0094 - acc: 0.9970 - val_loss: 0.8006 - val_acc: 0.8657
25000&#x2F;25000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 16s 644us&#x2F;step<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="./medias/loading1.gif" data-original="https://cdn.jsdelivr.net/gh/LiU-YU-HANG/blogpic@main/test1/202303022309219.png" alt=""></p>
<ul>
<li>分析精度图可以看出在后期，训练精度趋势下降，验证精度有下降趋势。</li>
<li>分析损失图可以看出在后期，训练损失值越来越小，验证损失越来越大。</li>
<li>由此可以看出增加神经元模型过拟化没有改变，并且精度下降，损失值提高。</li>
</ul>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">   ### 建少神经元 ###
# 分出验证集和训练集
x_val &#x3D; x_train[:10000]
partial_x_train &#x3D; x_train[10000:]

y_val &#x3D; y_train[:10000]
partial_y_train &#x3D; y_train[10000:]

model &#x3D; models.Sequential()
model.add(layers.Dense(4, activation&#x3D;&#39;relu&#39;, input_shape&#x3D;(10000,)))
model.add(layers.Dense(4, activation&#x3D;&#39;relu&#39;))
model.add(layers.Dense(1, activation&#x3D;&#39;sigmoid&#39;))

model.compile(optimizer&#x3D;&#39;rmsprop&#39;,
              loss&#x3D;&#39;binary_crossentropy&#39;,
              metrics&#x3D;[&#39;acc&#39;])

history &#x3D; model.fit(partial_x_train,
                    partial_y_train,
                    epochs&#x3D;20,
                    batch_size&#x3D;512,
                    validation_data&#x3D;(x_val, y_val))

results &#x3D; model.evaluate(x_test, y_test) #评估精度

history_dict &#x3D; history.history #得到训练历史数据，dict_keys([&#39;val_loss&#39;, &#39;val_acc&#39;, &#39;loss&#39;, &#39;acc&#39;])
loss_values &#x3D; history_dict[&#39;loss&#39;]
val_loss_valuse &#x3D; history_dict[&#39;val_loss&#39;]

acc &#x3D; history_dict[&#39;acc&#39;]
val_acc &#x3D; history_dict[&#39;val_acc&#39;]

epochs &#x3D; range(1, len(loss_values) + 1)

### 开始绘制 ###
# 绘制训练损失和验证损失图
plt.sca(plt.subplot(2,2,2))
plt.plot(epochs, loss_values, &#39;bo&#39;, label&#x3D;&#39;Training loss&#39;)
plt.plot(epochs, val_loss_valuse, &#39;b&#39;, label&#x3D;&#39;Validation loss&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;loss&#39;)
plt.title(&#39;Training and validation loss&#39;)
plt.legend()
# 绘制训练精度和验证精度图
plt.sca(plt.subplot(2,2,1))
plt.plot(epochs, val_acc, &#39;r&#39;, label&#x3D;&#39;Validation acc&#39;)
plt.plot(epochs, acc, &#39;ro&#39;, label&#x3D;&#39;Training acc&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;acc&#39;)
plt.title(&#39;Training and validation accuracy&#39;)
plt.legend()
# 绘制模型返回损失和精度图
plt.sca(plt.subplot(2,1,2))
name_list &#x3D; [&#39;results loss&#39;,&#39;results acc&#39;]
num_list &#x3D; results
plt.barh(range(len(num_list)), num_list,tick_label &#x3D; name_list)
plt.title(&#39;Results loss and accuracy&#39;)

plt.show()      <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">Train on 15000 samples, validate on 10000 samples
Epoch 1&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 12s 805us&#x2F;step - loss: 0.5984 - acc: 0.7784 - val_loss: 0.5225 - val_acc: 0.8469
Epoch 2&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 227us&#x2F;step - loss: 0.4588 - acc: 0.8727 - val_loss: 0.4295 - val_acc: 0.8628
Epoch 3&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 211us&#x2F;step - loss: 0.3675 - acc: 0.8943 - val_loss: 0.3665 - val_acc: 0.8739
Epoch 4&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 222us&#x2F;step - loss: 0.3008 - acc: 0.9079 - val_loss: 0.3248 - val_acc: 0.8837
Epoch 5&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 174us&#x2F;step - loss: 0.2499 - acc: 0.9227 - val_loss: 0.2965 - val_acc: 0.8886
Epoch 6&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 189us&#x2F;step - loss: 0.2122 - acc: 0.9358 - val_loss: 0.2860 - val_acc: 0.8877
Epoch 7&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 284us&#x2F;step - loss: 0.1843 - acc: 0.9439 - val_loss: 0.2786 - val_acc: 0.8892
Epoch 8&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 253us&#x2F;step - loss: 0.1626 - acc: 0.9509 - val_loss: 0.2725 - val_acc: 0.8897
Epoch 9&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 278us&#x2F;step - loss: 0.1436 - acc: 0.9569 - val_loss: 0.2771 - val_acc: 0.8889
Epoch 10&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 5s 307us&#x2F;step - loss: 0.1276 - acc: 0.9631 - val_loss: 0.2977 - val_acc: 0.8817
Epoch 11&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 261us&#x2F;step - loss: 0.1143 - acc: 0.9675 - val_loss: 0.2881 - val_acc: 0.8858
Epoch 12&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 193us&#x2F;step - loss: 0.1023 - acc: 0.9721 - val_loss: 0.2942 - val_acc: 0.8846
Epoch 13&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 187us&#x2F;step - loss: 0.0909 - acc: 0.9759 - val_loss: 0.3114 - val_acc: 0.8830
Epoch 14&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 192us&#x2F;step - loss: 0.0816 - acc: 0.9786 - val_loss: 0.3152 - val_acc: 0.8830
Epoch 15&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 178us&#x2F;step - loss: 0.0728 - acc: 0.9822 - val_loss: 0.3300 - val_acc: 0.8808
Epoch 16&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 213us&#x2F;step - loss: 0.0643 - acc: 0.9847 - val_loss: 0.3423 - val_acc: 0.8799
Epoch 17&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 206us&#x2F;step - loss: 0.0571 - acc: 0.9866 - val_loss: 0.3580 - val_acc: 0.8793
Epoch 18&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 191us&#x2F;step - loss: 0.0502 - acc: 0.9888 - val_loss: 0.3743 - val_acc: 0.8794
Epoch 19&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 197us&#x2F;step - loss: 0.0440 - acc: 0.9904 - val_loss: 0.3926 - val_acc: 0.8791
Epoch 20&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 181us&#x2F;step - loss: 0.0386 - acc: 0.9920 - val_loss: 0.4089 - val_acc: 0.8766
25000&#x2F;25000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 17s 662us&#x2F;step<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="./medias/loading1.gif" data-original="https://cdn.jsdelivr.net/gh/LiU-YU-HANG/blogpic@main/test1/202303022309585.png" alt=""></p>
<ul>
<li>分析精度图可以看出在后期，训练精度趋势下降，验证精度有上升，趋势减小。</li>
<li>分析损失图可以看出在后期，训练损失值越来越小，验证损失下降，趋势减小。</li>
<li>由此可以看出减小神经元模型过拟化减轻</li>
</ul>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">### 使用mse损失函数代替binary_crossentropy ###
# 分出验证集和训练集
x_val &#x3D; x_train[:10000]
partial_x_train &#x3D; x_train[10000:]

y_val &#x3D; y_train[:10000]
partial_y_train &#x3D; y_train[10000:]

model &#x3D; models.Sequential()
model.add(layers.Dense(16, activation&#x3D;&#39;relu&#39;, input_shape&#x3D;(10000,)))
model.add(layers.Dense(16, activation&#x3D;&#39;relu&#39;))
model.add(layers.Dense(1, activation&#x3D;&#39;sigmoid&#39;))

model.compile(optimizer&#x3D;&#39;rmsprop&#39;,
              loss&#x3D;&#39;mse&#39;,
              metrics&#x3D;[&#39;acc&#39;])

history &#x3D; model.fit(partial_x_train,
                    partial_y_train,
                    epochs&#x3D;20,
                    batch_size&#x3D;512,
                    validation_data&#x3D;(x_val, y_val))

results &#x3D; model.evaluate(x_test, y_test) #评估精度

history_dict &#x3D; history.history #得到训练历史数据，dict_keys([&#39;val_loss&#39;, &#39;val_acc&#39;, &#39;loss&#39;, &#39;acc&#39;])
loss_values &#x3D; history_dict[&#39;loss&#39;]
val_loss_valuse &#x3D; history_dict[&#39;val_loss&#39;]

acc &#x3D; history_dict[&#39;acc&#39;]
val_acc &#x3D; history_dict[&#39;val_acc&#39;]

epochs &#x3D; range(1, len(loss_values) + 1)

### 开始绘制 ###
# 绘制训练损失和验证损失图
plt.sca(plt.subplot(2,2,2))
plt.plot(epochs, loss_values, &#39;bo&#39;, label&#x3D;&#39;Training loss&#39;)
plt.plot(epochs, val_loss_valuse, &#39;b&#39;, label&#x3D;&#39;Validation loss&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;loss&#39;)
plt.title(&#39;Training and validation loss&#39;)
plt.legend()
# 绘制训练精度和验证精度图
plt.sca(plt.subplot(2,2,1))
plt.plot(epochs, val_acc, &#39;r&#39;, label&#x3D;&#39;Validation acc&#39;)
plt.plot(epochs, acc, &#39;ro&#39;, label&#x3D;&#39;Training acc&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;acc&#39;)
plt.title(&#39;Training and validation accuracy&#39;)
plt.legend()
# 绘制模型返回损失和精度图
plt.sca(plt.subplot(2,1,2))
name_list &#x3D; [&#39;results loss&#39;,&#39;results acc&#39;]
num_list &#x3D; results
plt.barh(range(len(num_list)), num_list,tick_label &#x3D; name_list)
plt.title(&#39;Results loss and accuracy&#39;)

plt.show()<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">Train on 15000 samples, validate on 10000 samples
Epoch 1&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 16s 1ms&#x2F;step - loss: 0.1694 - acc: 0.7905 - val_loss: 0.1192 - val_acc: 0.8717
Epoch 2&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 248us&#x2F;step - loss: 0.0916 - acc: 0.9047 - val_loss: 0.0947 - val_acc: 0.8847
Epoch 3&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 197us&#x2F;step - loss: 0.0656 - acc: 0.9290 - val_loss: 0.0926 - val_acc: 0.8793
Epoch 4&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 208us&#x2F;step - loss: 0.0508 - acc: 0.9443 - val_loss: 0.1012 - val_acc: 0.8605
Epoch 5&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 198us&#x2F;step - loss: 0.0416 - acc: 0.9553 - val_loss: 0.0835 - val_acc: 0.8870
Epoch 6&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 193us&#x2F;step - loss: 0.0338 - acc: 0.9658 - val_loss: 0.0922 - val_acc: 0.8728
Epoch 7&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 197us&#x2F;step - loss: 0.0281 - acc: 0.9722 - val_loss: 0.0907 - val_acc: 0.8784
Epoch 8&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 191us&#x2F;step - loss: 0.0231 - acc: 0.9786 - val_loss: 0.0883 - val_acc: 0.8799
Epoch 9&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 209us&#x2F;step - loss: 0.0198 - acc: 0.9827 - val_loss: 0.0901 - val_acc: 0.8784
Epoch 10&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 221us&#x2F;step - loss: 0.0161 - acc: 0.9869 - val_loss: 0.0960 - val_acc: 0.8718
Epoch 11&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 5s 354us&#x2F;step - loss: 0.0138 - acc: 0.9895 - val_loss: 0.1007 - val_acc: 0.8692
Epoch 12&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 299us&#x2F;step - loss: 0.0114 - acc: 0.9913 - val_loss: 0.1072 - val_acc: 0.8634
Epoch 13&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 228us&#x2F;step - loss: 0.0096 - acc: 0.9927 - val_loss: 0.1064 - val_acc: 0.8637
Epoch 14&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 201us&#x2F;step - loss: 0.0082 - acc: 0.9942 - val_loss: 0.1067 - val_acc: 0.8639
Epoch 15&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 208us&#x2F;step - loss: 0.0079 - acc: 0.9939 - val_loss: 0.1036 - val_acc: 0.8686
Epoch 16&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 207us&#x2F;step - loss: 0.0079 - acc: 0.9923 - val_loss: 0.1049 - val_acc: 0.8695
Epoch 17&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 211us&#x2F;step - loss: 0.0046 - acc: 0.9965 - val_loss: 0.1069 - val_acc: 0.8662
Epoch 18&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 206us&#x2F;step - loss: 0.0060 - acc: 0.9946 - val_loss: 0.1085 - val_acc: 0.8656
Epoch 19&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 216us&#x2F;step - loss: 0.0051 - acc: 0.9957 - val_loss: 0.1097 - val_acc: 0.8651
Epoch 20&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 190us&#x2F;step - loss: 0.0036 - acc: 0.9969 - val_loss: 0.1112 - val_acc: 0.8629
25000&#x2F;25000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 12s 478us&#x2F;step<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="./medias/loading1.gif" data-original="https://cdn.jsdelivr.net/gh/LiU-YU-HANG/blogpic@main/test1/202303022309728.png" alt=""></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">### 使用tanh激活 ###
# 分出验证集和训练集
x_val &#x3D; x_train[:10000]
partial_x_train &#x3D; x_train[10000:]

y_val &#x3D; y_train[:10000]
partial_y_train &#x3D; y_train[10000:]

model &#x3D; models.Sequential()
model.add(layers.Dense(16, activation&#x3D;&#39;tanh&#39;, input_shape&#x3D;(10000,)))
model.add(layers.Dense(16, activation&#x3D;&#39;tanh&#39;))
model.add(layers.Dense(1, activation&#x3D;&#39;sigmoid&#39;))

model.compile(optimizer&#x3D;&#39;rmsprop&#39;,
              loss&#x3D;&#39;mse&#39;,
              metrics&#x3D;[&#39;acc&#39;])

history &#x3D; model.fit(partial_x_train,
                    partial_y_train,
                    epochs&#x3D;20,
                    batch_size&#x3D;512,
                    validation_data&#x3D;(x_val, y_val))

results &#x3D; model.evaluate(x_test, y_test) #评估精度

history_dict &#x3D; history.history #得到训练历史数据，dict_keys([&#39;val_loss&#39;, &#39;val_acc&#39;, &#39;loss&#39;, &#39;acc&#39;])
loss_values &#x3D; history_dict[&#39;loss&#39;]
val_loss_valuse &#x3D; history_dict[&#39;val_loss&#39;]

acc &#x3D; history_dict[&#39;acc&#39;]
val_acc &#x3D; history_dict[&#39;val_acc&#39;]

epochs &#x3D; range(1, len(loss_values) + 1)

### 开始绘制 ###
# 绘制训练损失和验证损失图
plt.sca(plt.subplot(2,2,2))
plt.plot(epochs, loss_values, &#39;bo&#39;, label&#x3D;&#39;Training loss&#39;)
plt.plot(epochs, val_loss_valuse, &#39;b&#39;, label&#x3D;&#39;Validation loss&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;loss&#39;)
plt.title(&#39;Training and validation loss&#39;)
plt.legend()
# 绘制训练精度和验证精度图
plt.sca(plt.subplot(2,2,1))
plt.plot(epochs, val_acc, &#39;r&#39;, label&#x3D;&#39;Validation acc&#39;)
plt.plot(epochs, acc, &#39;ro&#39;, label&#x3D;&#39;Training acc&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;acc&#39;)
plt.title(&#39;Training and validation accuracy&#39;)
plt.legend()
# 绘制模型返回损失和精度图
plt.sca(plt.subplot(2,1,2))
name_list &#x3D; [&#39;results loss&#39;,&#39;results acc&#39;]
num_list &#x3D; results
plt.barh(range(len(num_list)), num_list,tick_label &#x3D; name_list)
plt.title(&#39;Results loss and accuracy&#39;)

plt.show()<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">Train on 15000 samples, validate on 10000 samples
Epoch 1&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 19s 1ms&#x2F;step - loss: 0.1587 - acc: 0.7894 - val_loss: 0.1073 - val_acc: 0.8735
Epoch 2&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 222us&#x2F;step - loss: 0.0791 - acc: 0.9106 - val_loss: 0.0935 - val_acc: 0.8743
Epoch 3&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 234us&#x2F;step - loss: 0.0527 - acc: 0.9388 - val_loss: 0.0877 - val_acc: 0.8796
Epoch 4&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 193us&#x2F;step - loss: 0.0416 - acc: 0.9509 - val_loss: 0.0836 - val_acc: 0.8861
Epoch 5&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 179us&#x2F;step - loss: 0.0322 - acc: 0.9631 - val_loss: 0.0878 - val_acc: 0.8824
Epoch 6&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 189us&#x2F;step - loss: 0.0243 - acc: 0.9727 - val_loss: 0.0979 - val_acc: 0.8720
Epoch 7&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 185us&#x2F;step - loss: 0.0199 - acc: 0.9779 - val_loss: 0.0965 - val_acc: 0.8744
Epoch 8&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 188us&#x2F;step - loss: 0.0179 - acc: 0.9791 - val_loss: 0.0998 - val_acc: 0.8751
Epoch 9&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 178us&#x2F;step - loss: 0.0141 - acc: 0.9838 - val_loss: 0.1026 - val_acc: 0.8751
Epoch 10&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 189us&#x2F;step - loss: 0.0133 - acc: 0.9856 - val_loss: 0.1056 - val_acc: 0.8718
Epoch 11&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 218us&#x2F;step - loss: 0.0070 - acc: 0.9937 - val_loss: 0.1133 - val_acc: 0.8634
Epoch 12&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 215us&#x2F;step - loss: 0.0101 - acc: 0.9883 - val_loss: 0.1098 - val_acc: 0.8699
Epoch 13&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 224us&#x2F;step - loss: 0.0081 - acc: 0.9909 - val_loss: 0.1261 - val_acc: 0.8567
Epoch 14&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 253us&#x2F;step - loss: 0.0056 - acc: 0.9943 - val_loss: 0.1135 - val_acc: 0.8680
Epoch 15&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 218us&#x2F;step - loss: 0.0081 - acc: 0.9912 - val_loss: 0.1144 - val_acc: 0.8683
Epoch 16&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 238us&#x2F;step - loss: 0.0044 - acc: 0.9957 - val_loss: 0.1154 - val_acc: 0.8668
Epoch 17&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 187us&#x2F;step - loss: 0.0091 - acc: 0.9893 - val_loss: 0.1182 - val_acc: 0.8652
Epoch 18&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 200us&#x2F;step - loss: 0.0040 - acc: 0.9961 - val_loss: 0.1166 - val_acc: 0.8681
Epoch 19&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 196us&#x2F;step - loss: 0.0039 - acc: 0.9961 - val_loss: 0.1186 - val_acc: 0.8658
Epoch 20&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 197us&#x2F;step - loss: 0.0090 - acc: 0.9899 - val_loss: 0.1186 - val_acc: 0.8670
25000&#x2F;25000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 12s 481us&#x2F;step<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="./medias/loading1.gif" data-original="https://cdn.jsdelivr.net/gh/LiU-YU-HANG/blogpic@main/test1/202303022310840.png" alt=""></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">### 使用tanh激活 ###
# 分出验证集和训练集
x_val &#x3D; x_train[:10000]
partial_x_train &#x3D; x_train[10000:]

y_val &#x3D; y_train[:10000]
partial_y_train &#x3D; y_train[10000:]

model &#x3D; models.Sequential()
model.add(layers.Dense(4, activation&#x3D;&#39;tanh&#39;, input_shape&#x3D;(10000,)))
#model.add(layers.Dense(4, activation&#x3D;&#39;tanh&#39;))
model.add(layers.Dense(1, activation&#x3D;&#39;sigmoid&#39;))

model.compile(optimizer&#x3D;&#39;rmsprop&#39;,
              loss&#x3D;&#39;mse&#39;,
              metrics&#x3D;[&#39;acc&#39;])

history &#x3D; model.fit(partial_x_train,
                    partial_y_train,
                    epochs&#x3D;20,
                    batch_size&#x3D;512,
                    validation_data&#x3D;(x_val, y_val))

results &#x3D; model.evaluate(x_test, y_test) #评估精度

history_dict &#x3D; history.history #得到训练历史数据，dict_keys([&#39;val_loss&#39;, &#39;val_acc&#39;, &#39;loss&#39;, &#39;acc&#39;])
loss_values &#x3D; history_dict[&#39;loss&#39;]
val_loss_valuse &#x3D; history_dict[&#39;val_loss&#39;]

acc &#x3D; history_dict[&#39;acc&#39;]
val_acc &#x3D; history_dict[&#39;val_acc&#39;]

epochs &#x3D; range(1, len(loss_values) + 1)

### 开始绘制 ###
# 绘制训练损失和验证损失图
plt.sca(plt.subplot(2,2,2))
plt.plot(epochs, loss_values, &#39;bo&#39;, label&#x3D;&#39;Training loss&#39;)
plt.plot(epochs, val_loss_valuse, &#39;b&#39;, label&#x3D;&#39;Validation loss&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;loss&#39;)
plt.title(&#39;Training and validation loss&#39;)
plt.legend()
# 绘制训练精度和验证精度图
plt.sca(plt.subplot(2,2,1))
plt.plot(epochs, val_acc, &#39;r&#39;, label&#x3D;&#39;Validation acc&#39;)
plt.plot(epochs, acc, &#39;ro&#39;, label&#x3D;&#39;Training acc&#39;)
plt.xlabel(&#39;Epochs&#39;)
plt.ylabel(&#39;acc&#39;)
plt.title(&#39;Training and validation accuracy&#39;)
plt.legend()
# 绘制模型返回损失和精度图
plt.sca(plt.subplot(2,1,2))
name_list &#x3D; [&#39;results loss&#39;,&#39;results acc&#39;]
num_list &#x3D; results
plt.barh(range(len(num_list)), num_list,tick_label &#x3D; name_list)
plt.title(&#39;Results loss and accuracy&#39;)

plt.show()<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">Train on 15000 samples, validate on 10000 samples
Epoch 1&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 13s 867us&#x2F;step - loss: 0.1855 - acc: 0.7857 - val_loss: 0.1552 - val_acc: 0.8361
Epoch 2&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 4s 240us&#x2F;step - loss: 0.1283 - acc: 0.8861 - val_loss: 0.1273 - val_acc: 0.8627
Epoch 3&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 178us&#x2F;step - loss: 0.1025 - acc: 0.9099 - val_loss: 0.1104 - val_acc: 0.8819
Epoch 4&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 172us&#x2F;step - loss: 0.0858 - acc: 0.9247 - val_loss: 0.1015 - val_acc: 0.8855
Epoch 5&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 175us&#x2F;step - loss: 0.0738 - acc: 0.9353 - val_loss: 0.0944 - val_acc: 0.8912
Epoch 6&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 188us&#x2F;step - loss: 0.0648 - acc: 0.9423 - val_loss: 0.0900 - val_acc: 0.8905
Epoch 7&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 186us&#x2F;step - loss: 0.0574 - acc: 0.9499 - val_loss: 0.0876 - val_acc: 0.8914
Epoch 8&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 171us&#x2F;step - loss: 0.0515 - acc: 0.9550 - val_loss: 0.0856 - val_acc: 0.8896
Epoch 9&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 2s 166us&#x2F;step - loss: 0.0462 - acc: 0.9601 - val_loss: 0.0844 - val_acc: 0.8898
Epoch 10&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 179us&#x2F;step - loss: 0.0419 - acc: 0.9639 - val_loss: 0.0848 - val_acc: 0.8872
Epoch 11&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 177us&#x2F;step - loss: 0.0378 - acc: 0.9689 - val_loss: 0.0839 - val_acc: 0.8874
Epoch 12&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 185us&#x2F;step - loss: 0.0346 - acc: 0.9716 - val_loss: 0.0833 - val_acc: 0.8876
Epoch 13&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 188us&#x2F;step - loss: 0.0313 - acc: 0.9761 - val_loss: 0.0837 - val_acc: 0.8852
Epoch 14&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 2s 162us&#x2F;step - loss: 0.0286 - acc: 0.9790 - val_loss: 0.0844 - val_acc: 0.8833
Epoch 15&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 2s 164us&#x2F;step - loss: 0.0260 - acc: 0.9807 - val_loss: 0.0846 - val_acc: 0.8850
Epoch 16&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 2s 160us&#x2F;step - loss: 0.0238 - acc: 0.9827 - val_loss: 0.0853 - val_acc: 0.8832
Epoch 17&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 173us&#x2F;step - loss: 0.0217 - acc: 0.9845 - val_loss: 0.0866 - val_acc: 0.8810
Epoch 18&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 205us&#x2F;step - loss: 0.0199 - acc: 0.9859 - val_loss: 0.0871 - val_acc: 0.8830
Epoch 19&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 3s 186us&#x2F;step - loss: 0.0182 - acc: 0.9879 - val_loss: 0.0890 - val_acc: 0.8772
Epoch 20&#x2F;20
15000&#x2F;15000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 2s 165us&#x2F;step - loss: 0.0167 - acc: 0.9886 - val_loss: 0.0890 - val_acc: 0.8806
25000&#x2F;25000 [&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;] - 13s 518us&#x2F;step<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="./medias/loading1.gif" data-original="https://cdn.jsdelivr.net/gh/LiU-YU-HANG/blogpic@main/test1/202303022310472.png" alt=""></p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/gitee.io/about" rel="external nofollow noreferrer">YuHang Liu</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://lyhcyt_admin.gitee.io/gitee.io/gitee.io/2022/04/05/dian-ying-ping-lun-fen-lei-er-fen-lei-wen-ti/">http://lyhcyt_admin.gitee.io/gitee.io/gitee.io/2022/04/05/dian-ying-ping-lun-fen-lei-er-fen-lei-wen-ti/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/gitee.io/about" target="_blank">YuHang Liu</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/gitee.io/tags/keras/">
                                    <span class="chip bg-color">keras</span>
                                </a>
                            
                                <a href="/gitee.io/tags/python/">
                                    <span class="chip bg-color">python</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/gitee.io/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/gitee.io/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="./medias/loading1.gif" data-original="/gitee.io/medias/reward/alipay.png" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="./medias/loading1.gif" data-original="/gitee.io/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/gitee.io/2022/04/05/ji-qi-xue-xi-ru-men/">
                    <div class="card-image">
                        
                        <img src="./medias/loading1.gif" data-original="https://cdn.jsdelivr.net/gh/LiU-YU-HANG/blogpic@main/test1/202303031442619.png" class="responsive-img" alt="机器学习入门">
                        
                        <span class="card-title">机器学习入门</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            说到深度学习，不可避免得会提及业界有哪些优秀的框架，Keras神经网络框架便是其中之一，它是一个高级神经网络APl，用Python编写，能够在TensorFlow，CNTK或Theano之上运行。它的开发重点是实现快速实验。能够以最小的延迟从理念到结果是进行良好研究的关键。接下来我们将要讲的神经网络原理与梯度求解，Keras都已经对它们有了很好的封装，在后续的学习中，大家只要学会怎么去构建网络结构就没有什么问题了，其余的问题都由神经网络框架替我们去解决。
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-04-05
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/gitee.io/categories/keras%E5%BF%AB%E9%80%9F%E6%90%AD%E5%BB%BA%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/" class="post-category">
                                    keras快速搭建神经网络
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/gitee.io/tags/keras/">
                        <span class="chip bg-color">keras</span>
                    </a>
                    
                    <a href="/gitee.io/tags/python/">
                        <span class="chip bg-color">python</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/gitee.io/2022/04/05/juan-ji-shen-jing-wang-luo-jian-jie/">
                    <div class="card-image">
                        
                        <img src="./medias/loading1.gif" data-original="https://cdn.jsdelivr.net/gh/LiU-YU-HANG/blogpic@main/test1/202303031442619.png" class="responsive-img" alt="卷积神经网络简介">
                        
                        <span class="card-title">卷积神经网络简介</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            说到深度学习，不可避免得会提及业界有哪些优秀的框架，Keras神经网络框架便是其中之一，它是一个高级神经网络APl，用Python编写，能够在TensorFlow，CNTK或Theano之上运行。它的开发重点是实现快速实验。能够以最小的延迟从理念到结果是进行良好研究的关键。接下来我们将要讲的神经网络原理与梯度求解，Keras都已经对它们有了很好的封装，在后续的学习中，大家只要学会怎么去构建网络结构就没有什么问题了，其余的问题都由神经网络框架替我们去解决。
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2022-04-05
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/gitee.io/categories/keras%E5%BF%AB%E9%80%9F%E6%90%AD%E5%BB%BA%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/" class="post-category">
                                    keras快速搭建神经网络
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/gitee.io/tags/keras/">
                        <span class="chip bg-color">keras</span>
                    </a>
                    
                    <a href="/gitee.io/tags/python/">
                        <span class="chip bg-color">python</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/gitee.io/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/gitee.io/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/gitee.io/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/gitee.io/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/gitee.io/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2022-2023</span>
            
            <a href="/gitee.io/about" target="_blank">YuHang Liu</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://gitee.com/lyhcyt_admin" class="tooltipped" target="_blank" data-tooltip="访问我的Gitee" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:1848450436@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1848450436" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1848450436" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/gitee.io/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/gitee.io/libs/materialize/materialize.min.js"></script>
    <script src="/gitee.io/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/gitee.io/libs/aos/aos.js"></script>
    <script src="/gitee.io/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/gitee.io/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/gitee.io/js/matery.js"></script>

    
        <!-- <script src='https://unpkg.com/mermaid@latest/dist/mermaid.min.js'></script> -->
        <script src='/gitee.io/libs/mermaid/mermaid.min.js'></script>
        <script>
          if (window.mermaid) {
            mermaid.initialize({theme: 'forest'});
          }
        </script>
    

    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
     
        <script type="text/javascript">
            // 只在桌面版网页启用特效
            var windowWidth = $(window).width();
            if (windowWidth > 768) {
                document.write('<script type="text/javascript" src="/gitee.io/libs/others/star.js"><\/script>');
            }
        </script>
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/gitee.io/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/gitee.io/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/gitee.io/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/gitee.io/libs/instantpage/instantpage.js" type="module"></script>
    


        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(r){r.imageLazyLoadSetting.processImages=t;var e=r.imageLazyLoadSetting.isSPA,n=r.imageLazyLoadSetting.preloadRatio||1,c=a();function a(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(){e&&(c=a());for(var t,o=0;o<c.length;o++)0<=(t=(t=c[o]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(r.innerHeight*n||document.documentElement.clientHeight*n)&&function(){var t,e,n,a,i=c[o];e=function(){c=c.filter(function(t){return i!==t}),r.imageLazyLoadSetting.onImageLoaded&&r.imageLazyLoadSetting.onImageLoaded(i)},(t=i).hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(n=new Image,a=t.getAttribute("data-original"),n.onload=function(){t.src=a,t.removeAttribute("data-original"),e&&e()},t.src!==a&&(n.src=a))}()}function i(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",i),r.addEventListener("resize",i),r.addEventListener("orientationchange",i)}(this);</script></body>

</html>
